
const { ccclass, property } = cc._decorator;

@ccclass
export default class ColorPanelV1 extends cc.Component {

    @property([cc.Component.EventHandler])
    callback: cc.Component.EventHandler[] = [];
    start() {
        this.notify(this.node.children[0])
        this.node.on(cc.Node.EventType.TOUCH_START, this.touchStart, this)
    }

    notify(element:cc.Node){
        for (let index = 0; index < this.callback.length; index++) {
            const callback = this.callback[index];
            callback.emit([element.color])
        }        
    }

    touchStart(e: cc.Touch) {
        let localtion = e.getLocation();
        this.node.convertToNodeSpaceAR(localtion, localtion)
        // console.log(' location ',localtion)
        let childNum = this.node.childrenCount;
        for (let index = 0; index < childNum; index++) {
            const element = this.node.children[index]
            let rect = element.getBoundingBox()
            // console.log(' rect ',rect)
            if (rect.contains(localtion)) {
                // cc.log(' color ',element.color)
                this.notify(element)
                break;
            }
        }
    }

    // update (dt) {}
}
